function showDiv(id) {
    $(id).style.visibility = "visible";
}

function closeDiv(id) {
    $(id).style.visibility = "hidden";
}

window.onload = function () {
    showClass();
}

function showClass() {
    axios.get("/class/findAll").then(resp => {
        let info = resp.data;
        let str = "";
        info.forEach(n => {
            str += `<tr><td>${n.name}</td><td>${n.time}</td><td>${n.teacher}</td>
<td><input type="button" value=" 删除按钮" onclick="delClass(${n.id})"> </td></tr>`;
        });
        $("data").innerHTML = str;
    });
}

function addClass() {
    axios.get("/class/add", {
        params: {
            name: $("classNameText").value,
            time: $("timeText").value,
            teacher: $("teacherText").value
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            showClass();
            showDiv('showInfoDiv');
            closeDiv('addClassDiv');
            $("showInfo").style.color = "black";
            $("showInfo").innerHTML = "添加成功";
        }
    })
}

function delClass(id) {
    axios.get("/class/del", {
        params: {
            id
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            showDiv('showInfoDiv');
            $("showInfo").style.color = "black";
            $("showInfo").innerHTML = "删除成功";
            showClass();
        } else {
            showDiv('showInfoDiv');
            $("showInfo").style.color = "red";
            $("showInfo").innerHTML = "删除失败班级内还有学生。";
        }
    });
}